package org.jeecg.modules.demo.hd.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.security.SecurityTools;
import org.jeecg.modules.demo.hd.entity.AppGoods;
import org.jeecg.modules.demo.hd.service.IAppGoodsService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 商品池管理
 * @Author: sutao
 * @Date:   2025-01-19
 * @Version: V1.0
 */
@Api(tags="领养牛信息")
@RestController
@RequestMapping("/hd/appGoods")
@Slf4j
public class AppGoodsController extends JeecgController<AppGoods, IAppGoodsService> {
	@Autowired
	private IAppGoodsService appGoodsService;
	
	/**
	 * 分页列表查询
	 *
	 * @param appGoods
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "app_goods-分页列表查询")
	@ApiOperation(value="认养列表查询", notes="认养列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<AppGoods>> queryPageList(AppGoods appGoods,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
        QueryWrapper<AppGoods> queryWrapper = QueryGenerator.initQueryWrapper(appGoods, req.getParameterMap());
		Page<AppGoods> page = new Page<AppGoods>(pageNo, pageSize);
		IPage<AppGoods> pageList = appGoodsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param appGoods
	 * @return
	 */
	@AutoLog(value = "添加")
	@ApiOperation(value="添加", notes="添加")
	@RequiresPermissions("hd:app_goods:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AppGoods appGoods) {
		LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
		appGoodsService.save(appGoods);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param appGoods
	 * @return
	 */
	@AutoLog(value = "编辑")
	@ApiOperation(value="编辑", notes="编辑")
	@RequiresPermissions("hd:app_goods:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AppGoods appGoods) {
		appGoodsService.updateById(appGoods);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "删除")
	@ApiOperation(value="删除", notes="删除")
	@RequiresPermissions("hd:app_goods:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		appGoodsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "批量删除")
	@ApiOperation(value="批量删除", notes="批量删除")
	@RequiresPermissions("hd:app_goods:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.appGoodsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "app_goods-通过id查询")
	@ApiOperation(value="详情", notes="详情")
	@GetMapping(value = "/queryById")
	public Result<AppGoods> queryById(@RequestParam(name="id",required=true) String id) {
		AppGoods appGoods = appGoodsService.getById(id);
		if(appGoods==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(appGoods);
	}


}
